package com.example.campsystem.controller;

import com.example.campsystem.entity.CustomApi;
import com.example.campsystem.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {
    private final FileService fileService;

    @PostMapping("/upload")
    public ResponseEntity<CustomApi> uploadFile(@RequestParam("file") MultipartFile file) {
        CustomApi customApi = new CustomApi();
        try {
            String fileUrl = fileService.uploadFile(file);
            customApi.setCode(200);
            customApi.setMsg("文件上传成功");
            customApi.setData(fileUrl);
            return new ResponseEntity<>(customApi, HttpStatus.OK);
        } catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("文件上传失败：" + e.getMessage());
            return new ResponseEntity<>(customApi, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}